package com.gamerole.plugin.mvvmhttp;


import com.gamerole.plugin.mvvmhttp.bean.RequestBean;

import java.io.*;

public class FileUtil {
    /**
     * 包绝对路径
     */
    public static String projectFilePath="/home/lv/AndroidStudioProjects/MintPay/prices/src/main/java/com/sskj/prices";
    /**
     * 包
     */
    public static String projectPackge="com.sskj.prices";
    public static String projectModelName="app";
    public static String presenterFilePath="app";
    static String HttpServiceFilePath = "/service/HttpService.kt";
    static String HttpConfigFilePath = "/http/HttpConfig.java";
    static String HttpPresenterFilePath = "/repository/";
    static String HttpBeanFilePath = "/bean/";

    public static void generateHttpConfig(RequestBean requestBean) {
        File file = new File(projectFilePath + HttpConfigFilePath);
        if (file.exists()) {
            String fileContent = getFileContent(projectFilePath + HttpConfigFilePath);
            if (fileContent.contains("public static final String " + requestBean.getConstantName())) {
                String s = fileContent.replaceAll("public static final String " + requestBean.getConstantName() + "=[\\s\\S].*?;", "public static final String " + requestBean.getConstantName() + "=\"" + requestBean.getRequestUrl() + "\";");
                try {
                    Utils.StringBufferDemo(file, s);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                String s = fileContent.replaceAll("}([\\s])*$", Utils.generateHttpConfig(requestBean) + "\n}");
                try {
                    Utils.StringBufferDemo(file, s);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 读取文件的内容
     * 读取指定文件的内容
     *
     * @param path 为要读取文件的绝对路径
     * @return 以行读取文件后的内容。
     * @since 1.0
     */
    public static final String getFileContent(String path) {
        String filecontent = "";
        try {
            File f = new File(path);
            if (f.exists()) {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(f), "utf-8");
                BufferedReader br = new BufferedReader(isr);
//                FileReader fr = new FileReader(path);
//                BufferedReader br = new BufferedReader(fr); //建立BufferedReader对象，并实例化为br
                String line = br.readLine(); //从文件读取一行字符串
                //判断读取到的字符串是否不为空
                while (line != null) {
                    filecontent += line + "\n";
                    line = br.readLine(); //从文件中继续读取一行数据
                }
                br.close(); //关闭BufferedReader对象
                isr.close(); //关闭文件
            }

        } catch (IOException e) {

        }
        return filecontent;
    }

    public static void generateHttpService(RequestBean requestBean) {
        File file = new File(projectFilePath + HttpServiceFilePath);
        if (file.exists()) {
            String fileContent = getFileContent(projectFilePath + HttpServiceFilePath);

            String s = fileContent.replaceAll("}([\\s])*$", Utils.generateHttpService(requestBean) + "\n}");
            try {
                Utils.StringBufferDemo(file, s);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
    public static void generateMvvmHttpService(RequestBean requestBean) {
        File file = new File(projectFilePath + HttpServiceFilePath);
        if (file.exists()) {
            String fileContent = getFileContent(projectFilePath + HttpServiceFilePath);

            String s = fileContent.replaceAll("}([\\s])*$", Utils.generateHttpService(requestBean) + "\n}");
            try {
                Utils.StringBufferDemo(file, s);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public static void generateRepository(RequestBean requestBean) {
        String filePath = projectFilePath + HttpPresenterFilePath + requestBean.getPresenterName() + ".kt";
        File file = new File(filePath);
        if (file.exists()) {
            String fileContent = getFileContent(filePath);


            String s = fileContent.replaceAll("}([\\s])*$", Utils.generateSimplePresenter(requestBean) + "\n}");
            try {
                Utils.StringBufferDemo(file, s);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public static void generateBean(RequestBean requestBean) {
        if (requestBean.isCreateModel()&&requestBean.getModelName()!=null) {
            String filePath = projectFilePath + HttpBeanFilePath + requestBean.getModelName() + ".java";
            File file = new File(filePath);
            if (!file.exists()) {
                try {

                    file.createNewFile();

                    String s = Utils.generateBean(requestBean);
                    Utils.StringBufferDemo(file, s);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
